package com.nhjf.mobile.controller.reviewPainting;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.codec.EncoderException;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.nhjf.common.page.Pagination;
import com.nhjf.common.util.StringUtil;
import com.nhjf.mobile.common.util.ImageTools;
import com.nhjf.mobile.common.util.SysProperties;
import com.nhjf.mobile.common.util.WebUtils;
import com.nhjf.mobile.common.util.WeixinConfig;
import com.nhjf.model.cn_nojd.Cnnojd;
import com.nhjf.model.student.Student;
import com.nhjf.model.system.AdminSession;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.CoursesType;
import com.nhjf.model.teachers.OrganizationHasStudent;
import com.nhjf.model.teachers.ReviewPainting;
import com.nhjf.model.teachers.ReviewPaintingCollection;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.studnet.StudentQo;
import com.nhjf.pojo.qo.system.AdminSessionQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.OrganizationHasStudentQo;
import com.nhjf.pojo.qo.teachers.ReviewPaintingCollectionQo;
import com.nhjf.pojo.qo.teachers.ReviewPaintingQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.cn_nojd.CnnojdService;
import com.nhjf.service.student.StudentService;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.CoursesTypeService;
import com.nhjf.service.teachers.OrganizationHasStudentService;
import com.nhjf.service.teachers.ReviewPaintingCollectionService;
import com.nhjf.service.teachers.ReviewPaintingService;
import com.nhjf.service.teachers.TeachersService;

import net.sf.json.JSONObject;

@RequestMapping("/reviewPainting")
@Controller
public class ReviewPaintingController {

	@Autowired
	private ReviewPaintingService reviewPaintingService;
	@Autowired
	private AdminSessionService adminSessionService;
	@Autowired
	private UserService userService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private TeachersService teachersService;
	@Autowired
	private CnnojdService cnnojdService;
	@Autowired
	private CoursesTypeService coursesTypeService;
	@Autowired
	private ReviewPaintingCollectionService reviewPaintingCollectionService;
	@Autowired
	private TeachersService teacherService;
	@Autowired
	private OrganizationHasStudentService hasStudentService;
	//公众号id
	static final String appid = "wx2aec8d2e0a62811f";
	//开发者密码
	static final String AppSecret = "09b8ee0139256ea160bd944123b1e88c";
	/**
	 * 获取access_token
	 */
	private static final String access_token_url = "https://api.weixin.qq.com/cgi-bin/token";
	/**
	 * 获取jsapi_ticket
	 */
	private static final String jsapi_ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
	/**
	 * 新增改画
	 * @param file
	 * @param artName
	 * @param token
	 * @param typeOne
	 * @param typeTwo
	 * @param teacherId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/reviewPainting_save")
	public String saveReviewPainting(@RequestParam("file") MultipartFile file,@RequestParam(value="artName", required=true)String artName,@RequestParam(value="token", required=true)String token,@RequestParam(value="typeOne", required=true)String typeOne,@RequestParam(value="typeTwo", required=true)String typeTwo,@RequestParam(value="teacherId", required=true)String teacherId) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {
				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				//				TeachersQo teachersQo=new TeachersQo();
				//				teachersQo.setUserId(user.getId());
				//				Teacher teacher= teachersService.queryUnique(teachersQo);
				//获取图片原始文件名
				String originalFilename = file.getOriginalFilename();
				//文件名使用当前时间
				String date = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
				// 获取上传图片的扩展名(jpg/png/...)
				String extension = FilenameUtils.getExtension(originalFilename);
				String[] regular = { "JPG", "GIF","PNG","JPEG" ,"BMP"};
				boolean flag = false;
				// 判断文件格式是否合法
				for (String str : regular) {
					if (extension.equalsIgnoreCase(str)) {
						flag = true;
					}
				}
				if (!flag) {
					resMap.put("code", "500");
					resMap.put("message", "上传图片格式不正确！");
					return JSONObject.fromObject(resMap).toString();
				}

				SysProperties sysProperties=SysProperties.getInstance();
				String imgPath=sysProperties.get("reviewPaintingPath");
				// 图片上传的相对路径（因为相对路径放到页面上就可以显示图片）

				String path =imgPath+user.getId()+"/" + date + "." + extension;
				//图片上传的绝对路径

				File dir = new File(path);
				if (!dir.exists()) {
					dir.mkdirs();
				}
				// 上传图片
				try {
					file.transferTo(new File(path));
					int width=ImageTools.getImgWidth(dir);
					int height=ImageTools.getImgHeight(dir);
					ServiceResultDto serviceResultDto =reviewPaintingService.createReviewPainting(user.getId()+"/" + date + "." + extension, artName, width, height, user.getId(),Long.parseLong(teacherId),Long.parseLong(typeOne),Long.parseLong(typeTwo));
					if(serviceResultDto.getSuccess()) {
						resMap.put("code", "200");
						resMap.put("message", "成功");
						return JSONObject.fromObject(resMap).toString();
					}
					resMap.put("code", "500");
					resMap.put("message", "操作失败");
					return JSONObject.fromObject(resMap).toString();
				} catch (IllegalStateException | IOException e) {
					e.printStackTrace();
					resMap.put("code", "500");
					resMap.put("message", "操作失败");
					return JSONObject.fromObject(resMap).toString();
				}

			}
		}
	}
	/**
	 * 改画
	 * @param file
	 * @param sccore
	 * @param status
	 * @param token
	 * @return
	 */
	@RequestMapping("/reviewPainting_reviewPainting")
	@ResponseBody
	public String reviewPainting(@RequestParam("file") MultipartFile file,String sccore,@RequestParam(value="token", required=true)String token,String reviewId,String totalcommentsound,@RequestParam(value="amrId", required=true)String amrId,@RequestParam(value="amrTime", required=true)String amrTime) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession==null) {
				resMap.put("code", "406");
				resMap.put("message", "不存在此账号！");
				return JSONObject.fromObject(resMap).toString();
			}else {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}else {
					if(file.getSize()<=0) {
						resMap.put("code", "500");
						resMap.put("message", "图片不能为空！");
						return JSONObject.fromObject(resMap).toString();
					}
					ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
					reviewPaintingQo.setSeachUser(true);
					reviewPaintingQo.setSeachTeacher(true);
					reviewPaintingQo.setSccoreSort(true);
					reviewPaintingQo.setReviewId(Long.parseLong(reviewId));
					ReviewPainting reviewPainting=reviewPaintingService.queryUnique(reviewPaintingQo);
					if(reviewPainting.getStatus()==2||reviewPainting.getStatus()==3) {
						resMap.put("code", "500");
						resMap.put("message", "已经更改过此画！");
						return JSONObject.fromObject(resMap).toString();
					}
					if(reviewPainting.getUser().getIsRole()==1 || reviewPainting.getUser().getIsRole()==3) {
						TeachersQo teachersQo=new TeachersQo();
						teachersQo.setSeachOrganization(true);
						teachersQo.setUserId(user.getId());
						Teacher teacher2=teacherService.queryUnique(teachersQo); 
						TeachersQo teachersQo2=new TeachersQo();
						teachersQo2.setSeachOrganization(true);
						teachersQo2.setSeachUser(true);
						teachersQo2.setUserId(reviewPainting.getUser().getId());
						Teacher teacher=  teacherService.queryUnique(teachersQo2);
						if(teacher2.getTraininginstitution().getId()==teacher.getTraininginstitution().getId()) {
							reviewPainting.setIsOrganizationPainting(1);
						}else {
							reviewPainting.setIsOrganizationPainting(2);
						}
					}else {
						TeachersQo teachersQo=new TeachersQo();
						teachersQo.setSeachOrganization(true);
						teachersQo.setUserId(user.getId());
						Teacher teacher2=teacherService.queryUnique(teachersQo); 
						OrganizationHasStudentQo hasStudentQo=new OrganizationHasStudentQo();
						StudentQo studentQo=new StudentQo();
						studentQo.setUserid(reviewPainting.getUser().getId());
						Student student= studentService.queryUnique(studentQo);
						hasStudentQo.setStudentId(student.getId());
						hasStudentQo.setOrganizationId(teacher2.getTraininginstitution().getId());
						OrganizationHasStudent hasStudent= hasStudentService.queryUnique(hasStudentQo);
						if(hasStudent==null||hasStudent.getOrganization()==null){
							reviewPainting.setIsOrganizationPainting(2);
						}else{
							if(hasStudent.getOrganization().getId()==teacher2.getTraininginstitution().getId()) {
								reviewPainting.setIsOrganizationPainting(1);
							}else {
								reviewPainting.setIsOrganizationPainting(2);
							}
						}

					}
					//获取图片原始文件名
					String originalFilename = file.getOriginalFilename();
					//文件名使用当前时间
					String date = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
					// 获取上传图片的扩展名(jpg/png/...)
					String extension = FilenameUtils.getExtension(originalFilename);
					String[] regular = { "JPG", "GIF","PNG","JPEG" ,"BMP"};
					boolean flag = false;
					// 判断文件格式是否合法
					for (String str : regular) {
						if (extension.equalsIgnoreCase(str)) {
							flag = true;
						}
					}
					if (!flag) {
						resMap.put("code", "500");
						resMap.put("message", "上传图片格式不正确！");
						return JSONObject.fromObject(resMap).toString();
					}

					SysProperties sysProperties=SysProperties.getInstance();
					String imgPath=sysProperties.get("reviewPaintingPath");
					// 图片上传的相对路径（因为相对路径放到页面上就可以显示图片）

					String path =imgPath+user.getId()+"/" + date + "." + extension;
					//图片上传的绝对路径

					File dir = new File(path);
					if (!dir.exists()) {
						dir.mkdirs();
					}
					// 上传图片
					try {
						file.transferTo(new File(path));

						//						if(teacher.getUser().getId()==user.getId()) {
						reviewPainting.setSccore(Integer.parseInt(sccore));
						reviewPainting.setReviewtime(new Date());
						reviewPainting.setTotalcommentsound(totalcommentsound);
						reviewPainting.setStatus(2);
						reviewPainting.setChangepaintingURL(user.getId()+"/" + date + "." + extension);
						if(StringUtils.isNotEmpty(amrId)){
							String accessToken="";

							String imagePath=sysProperties.get("VoiceFile")+"/"+user.getId()+"/";
							String dateName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
							if(WeixinConfig.getAccess_tokenCreatTime()!=null){
								long diff = new Date().getTime() - WeixinConfig.getAccess_tokenCreatTime().getTime();
								long s = diff/ (1000 * 60);
								if(s<100L){
									accessToken=WeixinConfig.getAccess_token();

									try {
										String code=saveImageToDisk(accessToken, amrId, dateName, imagePath);
										if("200".equals(code)){
											reviewPainting.setVoiceReview(user.getId()+"/"+dateName+".mp3");
											File armfile=new File(imagePath+dateName+".amr");
											if(armfile.exists()){
												armfile.delete();
											}
											reviewPainting.setVoiceDuration(Integer.parseInt(amrTime));
										}else{
											resMap.put("code", "500");
											resMap.put("message", "error");
											return JSONObject.fromObject(resMap).toString();
										}
									} catch (Exception e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
										resMap.put("code", "500");
										resMap.put("message", "error");
										return JSONObject.fromObject(resMap).toString();
									}

								}else{
									Map<String, Object> parameterMap = new TreeMap<>();
									parameterMap.put("appid", appid);
									parameterMap.put("grant_type", "client_credential");
									parameterMap.put("secret", AppSecret);
									String result = WebUtils.get(access_token_url, parameterMap);
									com.alibaba.fastjson.JSONObject json=com.alibaba.fastjson.JSONObject.parseObject(result);
									if(StringUtils.isNotEmpty(json.getString("access_token"))){
										accessToken=json.getString("access_token");

										try {
											String code=saveImageToDisk(accessToken, amrId, dateName, imagePath);
											if("200".equals(code)){
												reviewPainting.setVoiceReview(user.getId()+"/"+dateName+".mp3");
												reviewPainting.setVoiceDuration(Integer.parseInt(amrTime));
												File armfile=new File(imagePath+dateName+".amr");
												if(armfile.exists()){
													armfile.delete();
												}
											}else{
												resMap.put("code", "500");
												resMap.put("message", "error");
												return JSONObject.fromObject(resMap).toString();
											}
										} catch (Exception e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
											resMap.put("code", "500");
											resMap.put("message", "error");
											return JSONObject.fromObject(resMap).toString();
										}

									}else{
										resMap.put("code", "500");
										resMap.put("message", "获取accessToken失败");
										return JSONObject.fromObject(resMap).toString();
									}
								}
							}else{
								Map<String, Object> parameterMap = new TreeMap<>();
								parameterMap.put("appid", appid);
								parameterMap.put("grant_type", "client_credential");
								parameterMap.put("secret", AppSecret);
								String result = WebUtils.get(access_token_url, parameterMap);
								com.alibaba.fastjson.JSONObject json=com.alibaba.fastjson.JSONObject.parseObject(result);
								if(StringUtils.isNotEmpty(json.getString("access_token"))){
									accessToken=json.getString("access_token");

									try {
										String code=saveImageToDisk(accessToken, amrId, dateName, imagePath);
										if("200".equals(code)){
											reviewPainting.setVoiceReview(user.getId()+"/"+dateName+".mp3");
											reviewPainting.setVoiceDuration(Integer.parseInt(amrTime));
											File armfile=new File(imagePath+dateName+".amr");
											if(armfile.exists()){
												armfile.delete();
											}
										}else{
											resMap.put("code", "500");
											resMap.put("message", "error");
											return JSONObject.fromObject(resMap).toString();
										}
									} catch (Exception e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
										resMap.put("code", "500");
										resMap.put("message", "error");
										return JSONObject.fromObject(resMap).toString();
									}

								}else{
									resMap.put("code", "500");
									resMap.put("message", "获取accessToken失败");
									return JSONObject.fromObject(resMap).toString();
								}
							}
						}
						ReviewPainting reviewPainting2= reviewPaintingService.update(reviewPainting);
						if(reviewPainting2!=null) {
							resMap.put("code", "200");
							resMap.put("message", "成功");
							return JSONObject.fromObject(resMap).toString();
						}

						//						}else {
						//							resMap.put("code", "");
						//							resMap.put("message", "操作失败");
						//							return JSONObject.fromObject(resMap).toString();
						//						}

					} catch (IllegalStateException | IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						resMap.put("code", "500");
						resMap.put("message", "操作失败");
						return JSONObject.fromObject(resMap).toString();
					}
				}
			}
			resMap.put("code", "500");
			resMap.put("message", "操作失败");
			return JSONObject.fromObject(resMap).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常！");
			return JSONObject.fromObject(resMap).toString();
		}

	}

	/**
	 * 查询以评审的所有画作
	 * @return
	 * 
	 * sccoreType=1 80分到100分 ，2代表 70分到80分 ，3 60分到70分 ，4以批改 ，5全部
	 */
	@ResponseBody
	@RequestMapping("/reviewPainting_shouAll")
	public String showAllReviewPainting(@RequestParam(value="page", defaultValue="1")Integer page,@RequestParam(value="sccoreType", defaultValue="1")Integer sccoreType) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
			reviewPaintingQo.setSeachTeacher(true);
			//			reviewPaintingQo.setSccoreSort(true);
			reviewPaintingQo.setSccoreType(sccoreType);
			Pagination pagination = new Pagination();
			pagination.setPageSize(10);
			pagination.setPageNo(page);
			pagination.setCondition(reviewPaintingQo);
			pagination = reviewPaintingService.queryPagination(pagination);
			List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
			SysProperties sysProperties=SysProperties.getInstance();
			String imagePath=sysProperties.get("imagePath");
			SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			for (ReviewPainting reviewPainting : reviewPaintingList) {
				reviewPainting.setReleasedate(format1.format(reviewPainting.getReviewtime()));
				if(StringUtils.isNotEmpty(reviewPainting.getVoiceReview())){
					reviewPainting.setVoiceReview(imagePath+"/voice/"+reviewPainting.getVoiceReview());
				}
				if(!StringUtils.isEmpty(reviewPainting.getTeacher().getAvatar())) {
					if(reviewPainting.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {

					}else {
						reviewPainting.getTeacher().setAvatar(imagePath+"/picture/"+reviewPainting.getTeacher().getAvatar());
					}

				}
				if(!StringUtils.isEmpty(reviewPainting.getChangepaintingURL())) {
					reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
				}
				TeachersQo teachersQo1=new TeachersQo();
				teachersQo1.setTeacherId(reviewPainting.getTeacher().getId());
				teachersQo1.setSeachUser(true);
				Teacher teacher1=teachersService.queryUnique(teachersQo1);
				reviewPainting.getTeacher().setUserId(teacher1.getUser().getId());
			}
			if(reviewPaintingList!=null) {
				resMap.put("code", "200");
				resMap.put("message", pagination);
				return JSONObject.fromObject(resMap).toString();
			}else {
				resMap.put("code", "404");
				resMap.put("message", "暂时没有已评审的的画！");
				return JSONObject.fromObject(resMap).toString();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		resMap.put("code", "500");
		resMap.put("message", "系统异常");
		return JSONObject.fromObject(resMap).toString();

	}
	/**
	 * 查询改画各类学员分数当天第一
	 * @return
	 */
	@RequestMapping("/reviewPainting_type")
	@ResponseBody
	public String typeReviewPainting()  {
		Map<String, Object> resMap=new HashMap<String, Object>();
		Map<String, Object> resMap1=new HashMap<String, Object>();
		try {

			ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
			ReviewPaintingQo reviewPaintingQo1=new ReviewPaintingQo();
			ReviewPaintingQo reviewPaintingQo2=new ReviewPaintingQo();
			ReviewPaintingQo reviewPaintingQo3=new ReviewPaintingQo();
			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String fom= format.format(new Date())+" 00:00:00";
			Date str=format1.parse(fom);


			reviewPaintingQo.setSeachUser(true);
			reviewPaintingQo.setSeachTeacher(true);
			reviewPaintingQo.setReviewStartingTime(str);
			reviewPaintingQo.setTypeOne(2L);
			reviewPaintingQo.setStatus(2);
			//			reviewPaintingQo.setSccoreSort(true);

			reviewPaintingQo1.setSeachTeacher(true);
			reviewPaintingQo1.setSeachUser(true);
			reviewPaintingQo1.setReviewStartingTime(str);
			reviewPaintingQo1.setTypeOne(3L);
			reviewPaintingQo1.setStatus(2);
			//			reviewPaintingQo1.setSccoreSort(true);


			reviewPaintingQo2.setSeachUser(true);
			reviewPaintingQo2.setSeachTeacher(true);
			reviewPaintingQo2.setReviewStartingTime(str);
			reviewPaintingQo2.setTypeOne(4L);
			reviewPaintingQo2.setStatus(2);
			//			reviewPaintingQo2.setSccoreSort(true);

			reviewPaintingQo3.setSeachUser(true);
			reviewPaintingQo3.setSeachTeacher(true);
			reviewPaintingQo3.setReviewStartingTime(str);
			reviewPaintingQo3.setTypeOne(70L);
			reviewPaintingQo3.setStatus(2);

			ReviewPainting reviewPainting= reviewPaintingService.queryUnique(reviewPaintingQo); 
			SysProperties sysProperties=SysProperties.getInstance();
			String imagePath=sysProperties.get("imagePath");
			if(reviewPainting!=null) {
				if(reviewPainting.getUser().getIsRole()==2) {
					StudentQo qo=new StudentQo();
					qo.setSeachStudent(true);
					qo.setUserid(reviewPainting.getUser().getId());
					Student student= studentService.queryUnique(qo);
					if(!StringUtils.isEmpty(student.getAvatar())) {
						if(student.getAvatar().indexOf(imagePath+"/picture/")>=0) {

						}else {
							student.setAvatar(imagePath+"/picture/"+student.getAvatar());
							reviewPainting.setAvatar(student.getAvatar());
							
						}
					}
					reviewPainting.setNickName(student.getNickname());
					resMap1.put("2", reviewPainting);
				}else {
					
					TeachersQo teachersQo=new TeachersQo();
					teachersQo.setUserId(reviewPainting.getUser().getId());
					Teacher teacher=teacherService.queryUnique(teachersQo);
					if(!StringUtils.isEmpty(teacher.getAvatar())) {
						if(teacher.getAvatar().indexOf(imagePath+"/picture/")>=0) {

						}else {
							reviewPainting.setAvatar(imagePath+"/picture/"+teacher.getAvatar());
						}
					}
					reviewPainting.setNickName(teacher.getNickname());
					resMap1.put("2", reviewPainting);
				}
			}else {
				resMap1.put("2", "");
			}


			ReviewPainting reviewPainting1= reviewPaintingService.queryUnique(reviewPaintingQo1); 

			if(reviewPainting1!=null) {
				if(reviewPainting1.getUser().getIsRole()==2) {
					StudentQo qo=new StudentQo();
					qo.setSeachStudent(true);
					qo.setUserid(reviewPainting1.getUser().getId());
					Student student= studentService.queryUnique(qo);
					if(!StringUtils.isEmpty(student.getAvatar())) {
						if(student.getAvatar().indexOf(imagePath+"/picture/")>=0) {

						}else {
							student.setAvatar(imagePath+"/picture/"+student.getAvatar());
							reviewPainting1.setAvatar(student.getAvatar());
							
						}
					}
					reviewPainting1.setNickName(student.getNickname());
					resMap1.put("3", reviewPainting1);
				}else {
					TeachersQo teachersQo=new TeachersQo();
					teachersQo.setUserId(reviewPainting1.getUser().getId());
					Teacher teacher=teacherService.queryUnique(teachersQo);
					if(!StringUtils.isEmpty(teacher.getAvatar())) {
						if(teacher.getAvatar().indexOf(imagePath+"/picture/")>=0) {

						}else {
							reviewPainting1.setAvatar(imagePath+"/picture/"+teacher.getAvatar());
						}
					}
					reviewPainting1.setNickName(teacher.getNickname());
					resMap1.put("3", reviewPainting1);
				}
			}else {
				resMap1.put("3", "");
			}

			ReviewPainting reviewPainting2= reviewPaintingService.queryUnique(reviewPaintingQo2); 

			if(reviewPainting2!=null) {
				if(reviewPainting2.getUser().getIsRole()==2) {
					StudentQo qo=new StudentQo();
					qo.setSeachStudent(true);
					qo.setUserid(reviewPainting.getUser().getId());
					Student student= studentService.queryUnique(qo);
					if(!StringUtils.isEmpty(student.getAvatar())) {
						if(student.getAvatar().indexOf(imagePath+"/picture/")>=0) {

						}else {
							student.setAvatar(imagePath+"/picture/"+student.getAvatar());
							reviewPainting2.setAvatar(student.getAvatar());
							
						}
					}
					reviewPainting2.setNickName(student.getNickname());
					resMap1.put("4", reviewPainting2);
				}else {
					TeachersQo teachersQo=new TeachersQo();
					teachersQo.setUserId(reviewPainting2.getUser().getId());
					Teacher teacher=teacherService.queryUnique(teachersQo);
					if(!StringUtils.isEmpty(teacher.getAvatar())) {
						if(teacher.getAvatar().indexOf(imagePath+"/picture/")>=0) {

						}else {
							reviewPainting2.setAvatar(imagePath+"/picture/"+teacher.getAvatar());
						}
					}
					reviewPainting2.setNickName(teacher.getNickname());
					resMap1.put("4", reviewPainting2);
				}
			}else {
				resMap1.put("4", "");
			}

			ReviewPainting reviewPainting3= reviewPaintingService.queryUnique(reviewPaintingQo3); 

			if(reviewPainting3!=null) {
				if(reviewPainting3.getUser().getIsRole()==2) {
					StudentQo qo=new StudentQo();
					qo.setSeachStudent(true);
					qo.setUserid(reviewPainting.getUser().getId());
					Student student= studentService.queryUnique(qo);
					if(!StringUtils.isEmpty(student.getAvatar())) {
						if(student.getAvatar().indexOf(imagePath+"/picture/")>=0) {

						}else {
							student.setAvatar(imagePath+"/picture/"+student.getAvatar());
							reviewPainting3.setAvatar(student.getAvatar());
							
						}
					}
					reviewPainting3.setNickName(student.getNickname());
					resMap1.put("70", reviewPainting3);
				}else {
					TeachersQo teachersQo=new TeachersQo();
					teachersQo.setUserId(reviewPainting3.getUser().getId());
					Teacher teacher=teacherService.queryUnique(teachersQo);
					if(!StringUtils.isEmpty(teacher.getAvatar())) {
						if(teacher.getAvatar().indexOf(imagePath+"/picture/")>=0) {

						}else {
							reviewPainting3.setAvatar(imagePath+"/picture/"+teacher.getAvatar());
						}
					}
					reviewPainting3.setNickName(teacher.getNickname());
					resMap1.put("70", reviewPainting3);
				}
			}else {
				resMap1.put("70", "");
			}
			if(resMap1!=null) {
				resMap.put("code", "200");
				resMap.put("message", resMap1);
				return JSONObject.fromObject(resMap).toString();
			}else {
				resMap.put("code", "500");
				resMap.put("message", "系统异常！");
				return JSONObject.fromObject(resMap).toString();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		resMap.put("code", "500");
		resMap.put("message", "系统异常！");
		return JSONObject.fromObject(resMap).toString();
	}
	/**
	 * 查看改画详情
	 * @param reviewId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/reviewPainting_details")
	public String detailsReviewPainting(@RequestParam(value="reviewId", required=true)String reviewId,@RequestParam(value="token", required=false)String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
			reviewPaintingQo.setSeachUser(true);
			reviewPaintingQo.setSeachTeacher(true);
			reviewPaintingQo.setReviewId(Long.parseLong(reviewId));
			ReviewPainting reviewPainting= reviewPaintingService.queryUnique(reviewPaintingQo);
			SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(reviewPainting!=null) {
				if(reviewPainting.getReviewtime()!=null) {
					reviewPainting.setReleasedate(format1.format(reviewPainting.getReviewtime()));
				}
				CoursesType coursesTypeOne=coursesTypeService.get(reviewPainting.getTypeOne());
				CoursesType coursesTypeTwo=coursesTypeService.get(reviewPainting.getTypeTwo());
				if(coursesTypeOne!=null) {
					reviewPainting.setTypeOneString(coursesTypeOne.getCoursesType());
				}
				if(coursesTypeTwo!=null) {
					reviewPainting.setTypeTwoString(coursesTypeTwo.getCoursesType());
				}
			}else {
				resMap.put("code", "500");
				resMap.put("message", "查无此改画！");
				return JSONObject.fromObject(resMap).toString();
			}
			if(StringUtils.isNotBlank(token)) {
				AdminSessionQo adminSessionQo=new AdminSessionQo();
				adminSessionQo.setToken(token);
				AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
				if(adminSession==null) {

				}else {
					UserQo userQo=new UserQo();
					userQo.setUserName(adminSession.getLoginName());
					User user= userService.queryUnique(userQo);
					if(user==null) {

					}else {
						ReviewPaintingCollectionQo paintingCollectionQo=new ReviewPaintingCollectionQo();
						paintingCollectionQo.setUserId(user.getId());
						paintingCollectionQo.setReviewPaintingid(Long.parseLong(reviewId));
						ReviewPaintingCollection collection= reviewPaintingCollectionService.queryUnique(paintingCollectionQo);
						if(collection!=null) {
							reviewPainting.setIsCollectionPaintings(1);
						}else {
							reviewPainting.setIsCollectionPaintings(0);
						}
					}
				}
			}
			//			UserQo userQo=new UserQo();
			//			userQo.setUserId(reviewPainting.getUser().getId());
			//			userQo.setSeachRole(true);
			//			User user=userService.queryUnique(userQo);
			SysProperties sysProperties=SysProperties.getInstance();
			String imagePath=sysProperties.get("imagePath");
			reviewPainting.getUser().setUserPassword("");
			if(reviewPainting.getUser().getIsRole()==2) {
				StudentQo studentQo=new StudentQo();
				studentQo.setUserid(reviewPainting.getUser().getId());
				Student stu =studentService.queryUnique(studentQo);
				reviewPainting.getUser().setNickName(stu.getNickname());
				if(!StringUtils.isEmpty(stu.getAvatar())) {
					if(reviewPainting.getUser().getAvatar()!=null) {
						if(reviewPainting.getUser().getAvatar().indexOf(imagePath+"/picture/")>=0) {

						}else {
							reviewPainting.getUser().setAvatar(imagePath+"/picture/"+stu.getAvatar());
						}
					}

				}
				if(reviewPainting.getUser().getProvince()!=null) {
					Cnnojd province= cnnojdService.get(reviewPainting.getUser().getProvince());
					if(province!=null) {
						reviewPainting.getUser().setProvinceName(province.getName());
					}
				}
			}else {
				TeachersQo qo=new TeachersQo();
				qo.setUserId(reviewPainting.getUser().getId());
				Teacher teacher=teachersService.queryUnique(qo);
				reviewPainting.getUser().setNickName(teacher.getNickname());
				if(!StringUtils.isEmpty(teacher.getAvatar())) {
					if(reviewPainting.getUser().getAvatar()!=null) {
						if(reviewPainting.getUser().getAvatar().indexOf(imagePath+"/picture/")>=0) {

						}else {
							reviewPainting.getUser().setAvatar(imagePath+"/picture/"+teacher.getAvatar());
						}
					}

				}
				if(reviewPainting.getUser().getProvince()!=null) {
					Cnnojd province= cnnojdService.get(reviewPainting.getUser().getProvince());
					if(province!=null) {
						reviewPainting.getUser().setProvinceName(province.getName());
					}
				}
			}

			reviewPainting.getUser().setId(reviewPainting.getUser().getId());
			TeachersQo teachersQo=new TeachersQo();
			teachersQo.setTeacherId(reviewPainting.getTeacher().getId());
			teachersQo.setSeachUser(true);
			Teacher teacher =teachersService.queryUnique(teachersQo);
			reviewPainting.getTeacher().setUserId(teacher.getUser().getId());
			//			if(stu!=null) {
			//				if(stu.getUser().getProvince()!=null) {
			//					Cnnojd province= cnnojdService.get(stu.getUser().getProvince());
			//					if(province!=null) {
			//						stu.setProvince(province.getName());
			//					}
			//
			//				}
			//			}else if(teacher!=null) {
			//				if(teacher.getUser().getProvince()!=null) {
			//					Cnnojd province= cnnojdService.get(teacher.getUser().getProvince());
			//					if(province!=null) {
			//						teacher.setAddressprovince(province.getName());
			//					}
			//
			//				}
			//			}
			if(!StringUtils.isEmpty(reviewPainting.getChangepaintingURL())) {
				reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
			}
			if(!StringUtils.isEmpty(reviewPainting.getNotchangedURL())) {
				reviewPainting.setNotchangedURL(imagePath+"/reviewPainting/"+reviewPainting.getNotchangedURL());
			}
			if(!StringUtils.isEmpty(reviewPainting.getVoiceReview())) {
				reviewPainting.setVoiceReview(imagePath+"/voice/"+reviewPainting.getVoiceReview());
			}
			if(!StringUtils.isEmpty(reviewPainting.getTeacher().getAvatar())) {
				if(reviewPainting.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {

				}else {
					reviewPainting.getTeacher().setAvatar(imagePath+"/picture/"+reviewPainting.getTeacher().getAvatar());
				}

			}
			resMap.put("code", "200");
			resMap.put("message", reviewPainting);
			return JSONObject.fromObject(resMap).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "系统异常！");
			return JSONObject.fromObject(resMap).toString();
		}
	}


	/**
	 * 查看日榜
	 * @param typeOne
	 * @param datejr
	 * @param page
	 * @return
	 */
	@RequestMapping("/reviewPainting_typeview")
	@ResponseBody
	public String typeviewReviewPainting(String typeOne,@RequestParam(value="datejr", required=false)String datejr,@RequestParam(value = "page", defaultValue = "1")Integer page) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			ReviewPaintingQo paintingQo=new ReviewPaintingQo();
			SimpleDateFormat formats=new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(StringUtils.isNotBlank(datejr)) {
				String datejr2= datejr+" 00:00:00";
				String datejr1= datejr+" 23:59:59";
				System.out.println(datejr);
				if(!StringUtils.isEmpty(typeOne)) {
					paintingQo.setSeachUser(true);
					paintingQo.setSeachTeacher(true);
					paintingQo.setSccoreSort(true);
					paintingQo.setStatus(2);

					paintingQo.setReviewStartingTime(format1.parse(datejr2));
					paintingQo.setReviewEndTime(format1.parse(datejr1));
					paintingQo.setTypeOne(Long.parseLong(typeOne));
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(paintingQo);
					pagination = reviewPaintingService.queryPagination(pagination);
					List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					if(reviewPaintingList!=null) {
						for (ReviewPainting reviewPainting : reviewPaintingList) {
							reviewPainting.setReleasedate(format1.format(reviewPainting.getReviewtime()));
							if(StringUtils.isNotEmpty(reviewPainting.getVoiceReview())){
								reviewPainting.setVoiceReview(imagePath+"/voice/"+reviewPainting.getVoiceReview());
							}
							if(!StringUtils.isEmpty(reviewPainting.getTeacher().getAvatar())) {
								if(reviewPainting.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									reviewPainting.getTeacher().setAvatar(imagePath+"/picture/"+reviewPainting.getTeacher().getAvatar());
								}

							}
							if(!StringUtils.isEmpty(reviewPainting.getChangepaintingURL())) {
								reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
							}
							TeachersQo teachersQo1=new TeachersQo();
							teachersQo1.setTeacherId(reviewPainting.getTeacher().getId());
							teachersQo1.setSeachUser(true);
							Teacher teacher1=teachersService.queryUnique(teachersQo1);
							reviewPainting.getTeacher().setUserId(teacher1.getUser().getId());
						}
						resMap.put("code", "200");
						resMap.put("message",pagination);
						return JSONObject.fromObject(resMap).toString();
					}
				}else {
					paintingQo.setSeachUser(true);
					paintingQo.setSeachTeacher(true);
					paintingQo.setSccoreSort(true);
					paintingQo.setStatus(2);
					Date str=format1.parse(datejr);
					paintingQo.setReviewStartingTime(str);
					paintingQo.setTypeOne(2L);
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(paintingQo);
					pagination = reviewPaintingService.queryPagination(pagination);
					List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					if(reviewPaintingList!=null) {
						for (ReviewPainting reviewPainting : reviewPaintingList) {
							reviewPainting.setReleasedate(format1.format(reviewPainting.getReviewtime()));
							if(StringUtils.isNotEmpty(reviewPainting.getVoiceReview())){
								reviewPainting.setVoiceReview(imagePath+"/voice/"+reviewPainting.getVoiceReview());
							}
							if(!StringUtils.isEmpty(reviewPainting.getTeacher().getAvatar())) {
								if(reviewPainting.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									reviewPainting.getTeacher().setAvatar(imagePath+"/picture/"+reviewPainting.getTeacher().getAvatar());
								}

							}
							if(!StringUtils.isEmpty(reviewPainting.getChangepaintingURL())) {
								reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
							}
							TeachersQo teachersQo1=new TeachersQo();
							teachersQo1.setTeacherId(reviewPainting.getTeacher().getId());
							teachersQo1.setSeachUser(true);
							Teacher teacher1=teachersService.queryUnique(teachersQo1);
							reviewPainting.getTeacher().setUserId(teacher1.getUser().getId());
						}
						resMap.put("code", "200");
						resMap.put("message",pagination);
						return JSONObject.fromObject(resMap).toString();
					}
				}
			}else {
				if(!StringUtils.isEmpty(typeOne)) {
					String fom= formats.format(new Date())+" 00:00:00";
					Date str=format1.parse(fom);
					System.out.println(str);
					paintingQo.setSeachUser(true);
					paintingQo.setSeachTeacher(true);
					paintingQo.setSccoreSort(true);
					paintingQo.setStatus(2);
					paintingQo.setReviewStartingTime(str);
					paintingQo.setTypeOne(Long.parseLong(typeOne));
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(paintingQo);
					pagination = reviewPaintingService.queryPagination(pagination);
					List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					if(reviewPaintingList!=null) {
						for (ReviewPainting reviewPainting : reviewPaintingList) {
							reviewPainting.setReleasedate(format1.format(reviewPainting.getReviewtime()));
							if(StringUtils.isNotEmpty(reviewPainting.getVoiceReview())){
								reviewPainting.setVoiceReview(imagePath+"/voice/"+reviewPainting.getVoiceReview());
							}
							if(!StringUtils.isEmpty(reviewPainting.getTeacher().getAvatar())) {
								if(reviewPainting.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									reviewPainting.getTeacher().setAvatar(imagePath+"/picture/"+reviewPainting.getTeacher().getAvatar());
								}

							}
							if(!StringUtils.isEmpty(reviewPainting.getChangepaintingURL())) {
								reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
							}
							TeachersQo teachersQo1=new TeachersQo();
							teachersQo1.setTeacherId(reviewPainting.getTeacher().getId());
							teachersQo1.setSeachUser(true);
							Teacher teacher1=teachersService.queryUnique(teachersQo1);
							reviewPainting.getTeacher().setUserId(teacher1.getUser().getId());
						}
						resMap.put("code", "200");
						resMap.put("message",pagination);
						return JSONObject.fromObject(resMap).toString();
					}
				}else {
					String fom= formats.format(new Date())+" 00:00:00";
					Date str=format1.parse(fom);
					System.out.println(str);
					paintingQo.setSeachUser(true);
					paintingQo.setSeachTeacher(true);
					paintingQo.setSccoreSort(true);
					paintingQo.setStatus(2);
					paintingQo.setReviewStartingTime(str);
					paintingQo.setTypeOne(2L);
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(paintingQo);
					pagination = reviewPaintingService.queryPagination(pagination);
					List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					if(reviewPaintingList!=null) {
						for (ReviewPainting reviewPainting : reviewPaintingList) {
							reviewPainting.setReleasedate(format1.format(reviewPainting.getReviewtime()));
							if(StringUtils.isNotEmpty(reviewPainting.getVoiceReview())){
								reviewPainting.setVoiceReview(imagePath+"/voice/"+reviewPainting.getVoiceReview());
							}
							if(!StringUtils.isEmpty(reviewPainting.getTeacher().getAvatar())) {
								if(reviewPainting.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									reviewPainting.getTeacher().setAvatar(imagePath+"/picture/"+reviewPainting.getTeacher().getAvatar());
								}

							}
							if(!StringUtils.isEmpty(reviewPainting.getChangepaintingURL())) {
								reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
							}
							TeachersQo teachersQo1=new TeachersQo();
							teachersQo1.setTeacherId(reviewPainting.getTeacher().getId());
							teachersQo1.setSeachUser(true);
							Teacher teacher1=teachersService.queryUnique(teachersQo1);
							reviewPainting.getTeacher().setUserId(teacher1.getUser().getId());
						}
						resMap.put("code", "200");
						resMap.put("message",pagination);
						return JSONObject.fromObject(resMap).toString();
					}

				}
			}
			resMap.put("code", "500");
			resMap.put("message","系统异常！");
			return JSONObject.fromObject(resMap).toString();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message","系统异常！");
			return JSONObject.fromObject(resMap).toString();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message","系统异常！");
			return JSONObject.fromObject(resMap).toString();
		}

	}
	/**
	 * 查看周榜
	 * @param date
	 * @param deta1
	 * @param page
	 * @return
	 */
	@RequestMapping("/timeInterval")
	@ResponseBody
	public String getTimeInterval(String typeOne,String date,String date1,@RequestParam(value = "page", defaultValue = "1")Integer page) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			if(StringUtils.isEmpty(date) || StringUtils.isEmpty(date1)) {
				if(!StringUtils.isEmpty(typeOne)) {
					SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
					SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String fom= format.format(this.getThisWeekMonday(new Date()))+" 00:00:00";
					String fom1= format.format(this.getThisWeekSunday(new Date()))+" 23:59:59";
					Date str=format1.parse(fom);
					Date str1=format1.parse(fom1);
					ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
					reviewPaintingQo.setReviewStartingTime(str);
					reviewPaintingQo.setTypeOne(Long.parseLong(typeOne));
					reviewPaintingQo.setReviewEndTime(str1);
					reviewPaintingQo.setSeachTeacher(true);
					reviewPaintingQo.setSccoreSort(true);
					reviewPaintingQo.setStatus(2);
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(reviewPaintingQo);
					pagination = reviewPaintingService.queryPagination(pagination);
					List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					if(reviewPaintingList!=null) {
						for (ReviewPainting reviewPainting : reviewPaintingList) {
							reviewPainting.setReleasedate(format1.format(reviewPainting.getReviewtime()));
							if(StringUtils.isNotEmpty(reviewPainting.getVoiceReview())){
								reviewPainting.setVoiceReview(imagePath+"/voice/"+reviewPainting.getVoiceReview());
							}
							if(!StringUtils.isEmpty(reviewPainting.getTeacher().getAvatar())) {
								if(reviewPainting.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									reviewPainting.getTeacher().setAvatar(imagePath+"/picture/"+reviewPainting.getTeacher().getAvatar());
								}

							}
							if(!StringUtils.isEmpty(reviewPainting.getChangepaintingURL())) {
								reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
							}
							TeachersQo teachersQo1=new TeachersQo();
							teachersQo1.setTeacherId(reviewPainting.getTeacher().getId());
							teachersQo1.setSeachUser(true);
							Teacher teacher1=teachersService.queryUnique(teachersQo1);
							reviewPainting.getTeacher().setUserId(teacher1.getUser().getId());
						}
						resMap.put("code", "200");
						resMap.put("message",pagination);
						return JSONObject.fromObject(resMap).toString();
					}
				}else {
					SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
					SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String fom= format.format(this.getThisWeekMonday(new Date()))+" 00:00:00";
					String fom1= format.format(this.getThisWeekSunday(new Date()))+" 23:59:59";
					Date str=format1.parse(fom);
					Date str1=format1.parse(fom1);
					ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
					reviewPaintingQo.setReviewStartingTime(str);
					reviewPaintingQo.setTypeOne(2L);
					reviewPaintingQo.setSeachTeacher(true);
					reviewPaintingQo.setStatus(2);
					reviewPaintingQo.setSccoreSort(true);
					reviewPaintingQo.setReviewEndTime(str1);
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(reviewPaintingQo);
					pagination = reviewPaintingService.queryPagination(pagination);
					List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					if(reviewPaintingList!=null) {
						for (ReviewPainting reviewPainting : reviewPaintingList) {
							reviewPainting.setReleasedate(format1.format(reviewPainting.getReviewtime()));
							if(StringUtils.isNotEmpty(reviewPainting.getVoiceReview())){
								reviewPainting.setVoiceReview(imagePath+"/voice/"+reviewPainting.getVoiceReview());
							}
							if(!StringUtils.isEmpty(reviewPainting.getTeacher().getAvatar())) {
								if(reviewPainting.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									reviewPainting.getTeacher().setAvatar(imagePath+"/picture/"+reviewPainting.getTeacher().getAvatar());
								}

							}
							if(!StringUtils.isEmpty(reviewPainting.getChangepaintingURL())) {
								reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
							}
							TeachersQo teachersQo1=new TeachersQo();
							teachersQo1.setTeacherId(reviewPainting.getTeacher().getId());
							teachersQo1.setSeachUser(true);
							Teacher teacher1=teachersService.queryUnique(teachersQo1);
							reviewPainting.getTeacher().setUserId(teacher1.getUser().getId());
						}
						resMap.put("code", "200");
						resMap.put("message",pagination);
						return JSONObject.fromObject(resMap).toString();
					}
				}

			}else {
				if(!StringUtils.isEmpty(typeOne)) {
					SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String fom= date+" 00:00:00";
					String fom1= date1+" 23:59:59";
					Date str=format1.parse(fom);
					Date str1=format1.parse(fom1);
					ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
					reviewPaintingQo.setReviewStartingTime(str);
					reviewPaintingQo.setReviewEndTime(str1);
					reviewPaintingQo.setTypeOne(Long.parseLong(typeOne));
					reviewPaintingQo.setSeachTeacher(true);
					reviewPaintingQo.setSccoreSort(true);
					reviewPaintingQo.setStatus(2);
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(reviewPaintingQo);
					pagination = reviewPaintingService.queryPagination(pagination);
					List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					if(reviewPaintingList!=null) {
						for (ReviewPainting reviewPainting : reviewPaintingList) {
							reviewPainting.setReleasedate(format1.format(reviewPainting.getReviewtime()));
							if(StringUtils.isNotEmpty(reviewPainting.getVoiceReview())){
								reviewPainting.setVoiceReview(imagePath+"/voice/"+reviewPainting.getVoiceReview());
							}
							if(!StringUtils.isEmpty(reviewPainting.getTeacher().getAvatar())) {
								if(reviewPainting.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {
								}else {
									reviewPainting.getTeacher().setAvatar(imagePath+"/picture/"+reviewPainting.getTeacher().getAvatar());
								}

							}

							if(!StringUtils.isEmpty(reviewPainting.getChangepaintingURL())) {
								reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
							}
							TeachersQo teachersQo1=new TeachersQo();
							teachersQo1.setTeacherId(reviewPainting.getTeacher().getId());
							teachersQo1.setSeachUser(true);
							Teacher teacher1=teachersService.queryUnique(teachersQo1);
							reviewPainting.getTeacher().setUserId(teacher1.getUser().getId());
						}
						resMap.put("code", "200");
						resMap.put("message",pagination);
						return JSONObject.fromObject(resMap).toString();
					}
				}else {
					SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String fom= date+" 00:00:00";
					String fom1= date1+" 23:59:59";
					Date str=format1.parse(fom);
					Date str1=format1.parse(fom1);
					ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
					reviewPaintingQo.setReviewStartingTime(str);
					reviewPaintingQo.setReviewEndTime(str1);
					reviewPaintingQo.setSeachTeacher(true);
					reviewPaintingQo.setTypeOne(2L);
					reviewPaintingQo.setStatus(2);
					reviewPaintingQo.setSccoreSort(true);
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(reviewPaintingQo);
					pagination = reviewPaintingService.queryPagination(pagination);
					List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					if(reviewPaintingList!=null) {
						for (ReviewPainting reviewPainting : reviewPaintingList) {
							reviewPainting.setReleasedate(format1.format(reviewPainting.getReviewtime()));
							if(StringUtils.isNotEmpty(reviewPainting.getVoiceReview())){
								reviewPainting.setVoiceReview(imagePath+"/voice/"+reviewPainting.getVoiceReview());
							}
							if(!StringUtils.isEmpty(reviewPainting.getTeacher().getAvatar())) {
								if(reviewPainting.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									reviewPainting.getTeacher().setAvatar(imagePath+"/picture/"+reviewPainting.getTeacher().getAvatar());
								}

							}
							if(!StringUtils.isEmpty(reviewPainting.getChangepaintingURL())) {
								reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
							}
							TeachersQo teachersQo1=new TeachersQo();
							teachersQo1.setTeacherId(reviewPainting.getTeacher().getId());
							teachersQo1.setSeachUser(true);
							Teacher teacher1=teachersService.queryUnique(teachersQo1);
							reviewPainting.getTeacher().setUserId(teacher1.getUser().getId());
						}
						resMap.put("code", "200");
						resMap.put("message",pagination);
						return JSONObject.fromObject(resMap).toString();
					}
				}

			}

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		resMap.put("code", "500");
		resMap.put("message", "系统异常！");
		return JSONObject.fromObject(resMap).toString();
	}


	/**
	 * 改画拒绝
	 * @param token
	 * @param reviewId
	 * @return
	 */
	@RequestMapping("/reviewPainting_refuseReview")
	@ResponseBody
	public String refuseReview(@RequestParam(value="token", required=true)String token,String reviewId) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession==null) {
				resMap.put("code", "406");
				resMap.put("message", "不存在此账号！");
				return JSONObject.fromObject(resMap).toString();
			}else {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}else {
					ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
					reviewPaintingQo.setSeachTeacher(true);
					reviewPaintingQo.setSeachUser(true);
					reviewPaintingQo.setSccoreSort(true);
					reviewPaintingQo.setReviewId(Long.parseLong(reviewId));
					ReviewPainting reviewPainting= reviewPaintingService.queryUnique(reviewPaintingQo);
					TeachersQo teachersQo=new TeachersQo();
					teachersQo.setSeachUser(true);
					teachersQo.setTeacherId(reviewPainting.getTeacher().getId());
					Teacher teacher= teachersService.queryUnique(teachersQo);
					String teacherId=teacher.getUser().getId().toString();
					String userId=user.getId().toString();
					if(teacherId.equals(userId)) {
						reviewPainting.setStatus(3);
						reviewPaintingService.update(reviewPainting);
						resMap.put("code", "200");
						resMap.put("message", "成功！");
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "500");
						resMap.put("message", "当前登陆账号和操作账号不一致！");
						return JSONObject.fromObject(resMap).toString();
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "系统异常！");
			return JSONObject.fromObject(resMap).toString();
		}
	}
	/**
	 * 查看月榜的全部信息
	 * @param date
	 * @param deta1
	 * @param page
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getMonth")
	public String getMonth(String typeOne,String date,@RequestParam(value = "page", defaultValue = "1")Integer page) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(StringUtil.isNotNull(date)) {
				if(!StringUtils.isEmpty(typeOne)) {

					Date str=format1.parse(date+"-01 00:00:00");
					Date str1=this.getThisWeekMonthEnd(format1.parse(date+"-01 23:59:59"));
					ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
					reviewPaintingQo.setSeachUser(true);
					reviewPaintingQo.setSccoreSort(true);
					reviewPaintingQo.setSeachTeacher(true);
					reviewPaintingQo.setStatus(2);
					reviewPaintingQo.setTypeOne(Long.parseLong(typeOne));
					reviewPaintingQo.setReviewStartingTime(str);
					reviewPaintingQo.setReviewEndTime(str1);
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(reviewPaintingQo);
					pagination = reviewPaintingService.queryPagination(pagination);
					List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					if(reviewPaintingList!=null) {
						for (ReviewPainting reviewPainting : reviewPaintingList) {
							reviewPainting.setReleasedate(format1.format(reviewPainting.getReviewtime()));
							if(StringUtils.isNotEmpty(reviewPainting.getVoiceReview())){
								reviewPainting.setVoiceReview(imagePath+"/voice/"+reviewPainting.getVoiceReview());
							}
							if(!StringUtils.isEmpty(reviewPainting.getTeacher().getAvatar())) {
								if(reviewPainting.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									reviewPainting.getTeacher().setAvatar(imagePath+"/picture/"+reviewPainting.getTeacher().getAvatar());
								}

							}
							if(!StringUtils.isEmpty(reviewPainting.getChangepaintingURL())) {
								reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
							}
							TeachersQo teachersQo1=new TeachersQo();
							teachersQo1.setTeacherId(reviewPainting.getTeacher().getId());
							teachersQo1.setSeachUser(true);
							Teacher teacher1=teachersService.queryUnique(teachersQo1);
							reviewPainting.getTeacher().setUserId(teacher1.getUser().getId());
						}
						resMap.put("code", "200");
						resMap.put("message",pagination);
						return JSONObject.fromObject(resMap).toString();
					}
				}else {

					Date str=format1.parse(date+"-01 00:00:00");
					Date str1=this.getThisWeekMonthEnd(format1.parse(date+"-01 23:59:59"));
					ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
					reviewPaintingQo.setSeachUser(true);
					reviewPaintingQo.setSeachTeacher(true);
					reviewPaintingQo.setSccoreSort(true);
					reviewPaintingQo.setStatus(2);
					reviewPaintingQo.setTypeOne(2L);
					reviewPaintingQo.setReviewStartingTime(str);
					reviewPaintingQo.setReviewEndTime(str1);
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(reviewPaintingQo);
					pagination = reviewPaintingService.queryPagination(pagination);
					List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					if(reviewPaintingList!=null) {
						for (ReviewPainting reviewPainting : reviewPaintingList) {
							reviewPainting.setReleasedate(format1.format(reviewPainting.getReviewtime()));
							if(StringUtils.isNotEmpty(reviewPainting.getVoiceReview())){
								reviewPainting.setVoiceReview(imagePath+"/voice/"+reviewPainting.getVoiceReview());
							}
							if(!StringUtils.isEmpty(reviewPainting.getTeacher().getAvatar())) {
								if(reviewPainting.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									reviewPainting.getTeacher().setAvatar(imagePath+"/picture/"+reviewPainting.getTeacher().getAvatar());
								}

							}
							if(!StringUtils.isEmpty(reviewPainting.getChangepaintingURL())) {
								reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
							}
							TeachersQo teachersQo1=new TeachersQo();
							teachersQo1.setTeacherId(reviewPainting.getTeacher().getId());
							teachersQo1.setSeachUser(true);
							Teacher teacher1=teachersService.queryUnique(teachersQo1);
							reviewPainting.getTeacher().setUserId(teacher1.getUser().getId());
						}
						resMap.put("code", "200");
						resMap.put("message",pagination);
						return JSONObject.fromObject(resMap).toString();
					}
				}

			}else {
				if(!StringUtils.isEmpty(typeOne)) {
					String fom= format1.format(this.getThisWeekMondayySta(new Date()));
					String fom1= format1.format(this.getThisWeekMonthEnd(new Date()));
					System.out.println(fom);
					System.out.println(fom1);
					Date str=format1.parse(fom);
					Date str1=format1.parse(fom1);
					ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
					reviewPaintingQo.setSeachUser(true);
					reviewPaintingQo.setSeachTeacher(true);
					reviewPaintingQo.setSccoreSort(true);
					reviewPaintingQo.setStatus(2);
					reviewPaintingQo.setTypeOne(Long.parseLong(typeOne));
					reviewPaintingQo.setReviewStartingTime(str);
					reviewPaintingQo.setReviewEndTime(str1);
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(reviewPaintingQo);
					pagination = reviewPaintingService.queryPagination(pagination);
					List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					if(reviewPaintingList!=null) {
						for (ReviewPainting reviewPainting : reviewPaintingList) {
							reviewPainting.setReleasedate(format1.format(reviewPainting.getReviewtime()));
							if(StringUtils.isNotEmpty(reviewPainting.getVoiceReview())){
								reviewPainting.setVoiceReview(imagePath+"/voice/"+reviewPainting.getVoiceReview());
							}
							if(!StringUtils.isEmpty(reviewPainting.getTeacher().getAvatar())) {
								if(reviewPainting.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									reviewPainting.getTeacher().setAvatar(imagePath+"/picture/"+reviewPainting.getTeacher().getAvatar());
								}

							}
							if(!StringUtils.isEmpty(reviewPainting.getChangepaintingURL())) {
								reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
							}
							TeachersQo teachersQo1=new TeachersQo();
							teachersQo1.setTeacherId(reviewPainting.getTeacher().getId());
							teachersQo1.setSeachUser(true);
							Teacher teacher1=teachersService.queryUnique(teachersQo1);
							reviewPainting.getTeacher().setUserId(teacher1.getUser().getId());
						}
						resMap.put("code", "200");
						resMap.put("message",pagination);
						return JSONObject.fromObject(resMap).toString();
					}

				}else {
					String fom= format1.format(this.getThisWeekMondayySta(new Date()));
					String fom1= format1.format(this.getThisWeekMonthEnd(new Date()));
					System.out.println(fom);
					System.out.println(fom1);
					Date str=format1.parse(fom);
					Date str1=format1.parse(fom1);
					ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
					reviewPaintingQo.setSeachUser(true);
					reviewPaintingQo.setSeachTeacher(true);
					reviewPaintingQo.setStatus(2);
					reviewPaintingQo.setTypeOne(2L);
					reviewPaintingQo.setSccoreSort(true);
					reviewPaintingQo.setReviewStartingTime(str);
					reviewPaintingQo.setReviewEndTime(str1);
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(reviewPaintingQo);
					pagination = reviewPaintingService.queryPagination(pagination);
					List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					if(reviewPaintingList!=null) {
						for (ReviewPainting reviewPainting : reviewPaintingList) {
							reviewPainting.setReleasedate(format1.format(reviewPainting.getReviewtime()));
							if(StringUtils.isNotEmpty(reviewPainting.getVoiceReview())){
								reviewPainting.setVoiceReview(imagePath+"/voice/"+reviewPainting.getVoiceReview());
							}
							if(!StringUtils.isEmpty(reviewPainting.getTeacher().getAvatar())) {
								if(reviewPainting.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									reviewPainting.getTeacher().setAvatar(imagePath+"/picture/"+reviewPainting.getTeacher().getAvatar());
								}

							}
							if(!StringUtils.isEmpty(reviewPainting.getChangepaintingURL())) {
								reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
							}
							TeachersQo teachersQo1=new TeachersQo();
							teachersQo1.setTeacherId(reviewPainting.getTeacher().getId());
							teachersQo1.setSeachUser(true);
							Teacher teacher1=teachersService.queryUnique(teachersQo1);
							reviewPainting.getTeacher().setUserId(teacher1.getUser().getId());
						}
						resMap.put("code", "200");
						resMap.put("message",pagination);
						return JSONObject.fromObject(resMap).toString();
					}
				}
			}


		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		resMap.put("code", "500");
		resMap.put("message", "异常！");
		return JSONObject.fromObject(resMap).toString();

	}

	public static Date getThisWeekMondayySta(Date date) {
		Calendar cal = Calendar.getInstance();	
		cal.setTime(date);	
		cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 



		return cal.getTime();	
	}

	public static Date getThisWeekMonthEnd(Date date) {
		Calendar cal = Calendar.getInstance();	
		cal.setTime(date);	
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));  


		return cal.getTime();	


	}


	public static Date getThisWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();	
		cal.setTime(date);	
		// 获得当前日期是一个星期的第几天		
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);	
		if (1 == dayWeek) {		
			cal.add(Calendar.DAY_OF_MONTH, -1);	
		}
		// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一	
		cal.setFirstDayOfWeek(Calendar.MONDAY);		
		// 获得当前日期是一个星期的第几天	
		int day = cal.get(Calendar.DAY_OF_WEEK);		
		// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值	
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);	
		return cal.getTime();	


	}

	public static Date getThisWeekSunday(Date date) {
		Calendar cal = Calendar.getInstance();	
		cal.setTime(date);	
		// 获得当前日期是一个星期的第几天		
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);	
		if (1 == dayWeek) {		
			cal.add(Calendar.DAY_OF_MONTH, -1);	
		}
		// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一	
		cal.setFirstDayOfWeek(Calendar.SUNDAY);		
		// 获得当前日期是一个星期的第几天	
		int day = cal.get(Calendar.DAY_OF_WEEK);		
		// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值	
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek()+7 - day);	
		return cal.getTime();	

	}

	/**
	 * 
	 * 获取下载音频信
	 * 
	 * 
	 * 
	 * @param mediaId
	 * 
	 *            文件的id
	 * 
	 * @throws Exception
	 */
 
	public static String saveImageToDisk(String accessToken, String mediaId, String picName, String picPath)
			throws Exception {
		InputStream inputStream = getInputStream(accessToken, mediaId);
		byte[] data = new byte[10240];
		int len = 0;
		FileOutputStream fileOutputStream = null;
		try {
			File dir = new File(picPath);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			fileOutputStream = new FileOutputStream(picPath+picName+".amr");
			while ((len = inputStream.read(data)) != -1) {
				fileOutputStream.write(data, 0, len);
			}
			String code=changeAmrToMp3(picPath+picName+".amr", picPath+picName+".mp3");
			if("200".equals(code)){
				return "200";
			}else{
				return "500";
			}
			
		} catch (IOException e) {
			
			e.printStackTrace();
			return "500";
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
	/**
	 * 
	 * @Title: changeToMp3
	 * @Description: amr转mp3
	 * @param sourcePath 被转文件地址
	 * @param targetPath 转换后保存地址
	 */
	public static String changeAmrToMp3(String sourcePath, String targetPath) throws IllegalArgumentException, EncoderException {
				//获取文件地址。追踪到软件所在文件夹ji
				SysProperties sysProperties=SysProperties.getInstance();
				String webroot=sysProperties.get("ffmpeg_path");
	
	
				Runtime run = null;
				try {
					run = Runtime.getRuntime();
					long start = System.currentTimeMillis();
					System.out.println(new File(webroot).getAbsolutePath());
					// 执行ffmpeg.exe,前面是ffmpeg.exe的地址，中间是需要转换的文件地址，后面是转换后的文件地址。-i是转  换方式，意思是可编码解码，mp3编码方式采用的是libmp3lame
					Process p = run.exec(new File(webroot).getAbsolutePath() + " -i " + sourcePath
							+ " -acodec libmp3lame " + targetPath);
			        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String line = null;
                    StringBuilder result = new StringBuilder();
                    while ((line = br.readLine()) != null) {
                        System.out.println(line);
                        }
                	p.waitFor();
					// 释放进程
					p.getOutputStream().close();
					p.getInputStream().close();
					p.destroy();
//					p.getErrorStream().close();
				
					long end = System.currentTimeMillis();
					System.out.println(sourcePath + " convert success, costs:" + (end - start) + "ms");
					return "200";
				} catch (Exception e) {
					e.printStackTrace();
					return "400";
				} finally {
					// run调用lame解码器最后释放内存
					run.freeMemory();
				}
	}
	/**
	 * 
	 * 根据文件id下载文件
	 * 
	 * 
	 * 
	 * @param mediaId
	 * 
	 *            媒体id
	 * 
	 * @throws Exception
	 */
 
	public static InputStream getInputStream(String accessToken, String mediaId) {
		InputStream is = null;
		String url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token="
				+ accessToken + "&media_id=" + mediaId;
		try {
			URL urlGet = new URL(url);
			HttpURLConnection http = (HttpURLConnection) urlGet
					.openConnection();
			http.setRequestMethod("GET"); // 必须是get方式请求
			http.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			http.setDoOutput(true);
			http.setDoInput(true);
			System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
			System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
			http.connect();
			// 获取文件转化为byte流
			is = http.getInputStream();
 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return is;
	}
}
